package com.tianheng.wanzhou.datacollecting.netty.zhengjing.receiver.receiverImpl;

import com.baomidou.mybatisplus.extension.service.IService;
import com.tianheng.wanzhou.datacollecting.DataCollectingApplication;
import com.tianheng.wanzhou.datacollecting.db.entity.HuafangZhengjing;
import com.tianheng.wanzhou.datacollecting.db.entity.HuafangZhengjingHis;
import com.tianheng.wanzhou.datacollecting.db.service.IHuafangZhengjingHisService;
import com.tianheng.wanzhou.datacollecting.db.service.IHuafangZhengjingService;
import com.tianheng.wanzhou.datacollecting.netty.zhengjing.receiver.Receiver;
import com.tianheng.wanzhou.datacollecting.util.Data;
import com.tianheng.wanzhou.datacollecting.util.FloatOperate;
import com.tianheng.wanzhou.datacollecting.util.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: yujw
 * @Date: 2020/5/19 13:10
 * @Version 1.0
 */
@Component
public class HuafReceiver extends Receiver {

    //解析成小数
    Integer[] digital = new Integer[]{3, 23};
    //解析成string
    Integer[] str = new Integer[]{30};

    @Autowired
    IHuafangZhengjingService service;

    @Autowired
    IHuafangZhengjingHisService hisService;

    @Override
    public Object parseMessage(String message, String ip, Integer company, Integer workshop, String machineId) {
        Map<String, Data> map = new HashMap<>();
        map.put("machineIp", new Data(ip));
        map.put("company", new Data(company));
        map.put("workshop", new Data(workshop));

        Class huafang = HuafangZhengjing.class;
        Field[] fields = huafang.getDeclaredFields();
        Integer i = 0;
        for (Field field : fields) {
            //解析浮点数
            if (hasInt(i, digital)) {
                map.put(field.getName(), new Data(FloatOperate.operate(message.substring(8 * i, 8 * (i + 1)))));
            } else if (hasInt(i, str)) {
                //解析字符串
                map.put(field.getName(), new Data(new String(StrUtil.hexStr2Byte(message.substring(8 * i, 8 * (i + 5))))));
                break;
            } else {
                map.put(field.getName(), new Data(StrUtil.reverseHex2Int(message.substring(8 * i, 8 * (i + 1)))));
            }
            i+=1;
        }
        String warn1 = message.substring(280, 288);
        String warn2 = message.substring(288, 296);
        String warn3 = message.substring(296, 304);
        map.put("bianpinErr", StrUtil.getStatus(warn1, 0));
        map.put("paikaipaiheErr", StrUtil.getStatus(warn1, 1));
        map.put("airForceErr", StrUtil.getStatus(warn2, 0));
        map.put("stopBtnErr", StrUtil.getStatus(warn2, 1));
        map.put("coderSignErr", StrUtil.getStatus(warn2, 2));
        map.put("speedZeroErr", StrUtil.getStatus(warn2, 3));
        map.put("warpClipErr", StrUtil.getStatus(warn2, 4));
        map.put("yagunyashaErr", StrUtil.getStatus(warn2, 5));
        map.put("oilPumpMotorErr", StrUtil.getStatus(warn2, 8));
        map.put("oilBlockErr", StrUtil.getStatus(warn2, 9));
        map.put("oilForceErr", StrUtil.getStatus(warn2, 10));
        map.put("oilPumpErr", StrUtil.getStatus(warn2, 11));
        map.put("oilForceAdjustErr", StrUtil.getStatus(warn2, 12));
        map.put("internetErr", StrUtil.getStatus(warn3, 0));
        map.put("shajiaErr", StrUtil.getStatus(warn2, 1));
        map.put("warpStopBtn", StrUtil.getStatus(warn2, 8));
        map.put("tongzijiaStopBtn", StrUtil.getStatus(warn3, 9));
        map.put("duanshaErr", StrUtil.getStatus(warn3, 10));
        map.put("getLength", StrUtil.getStatus(warn3, 11));
        map.put("getMidLength", StrUtil.getStatus(warn3, 12));
        map.put("missionComlete", StrUtil.getStatus(warn3, 13));
        try{
            map.put("forceRodLocation", new Data(StrUtil.hexStr2Int(message.substring(400, 408))));
            map.put("breakDelayOpen", new Data(StrUtil.hexStr2Int(message.substring(408, 416))));
            map.put("breakDelayClose", new Data(StrUtil.hexStr2Int(message.substring(416, 424))));
            map.put("autoStopDelay", new Data(StrUtil.hexStr2Int(message.substring(424, 432))));
            map.put("sensitivityDelay", new Data(StrUtil.hexStr2Int(message.substring(432, 440))));
        }catch (Exception e){
            System.err.println("");
        }

        map.put("machineId", new Data(machineId));

        HuafangZhengjing huafangZhengjing = (HuafangZhengjing) Data.bind(new HuafangZhengjing(), map);
        HuafangZhengjingHis lastRecord = DataCollectingApplication.huafangZhengjingHis.get(ip);
        double now = huafangZhengjing.getAllLength();
        double last = 0.00;
        if (null != lastRecord) {
            last = lastRecord.getAllLength();
            //加断头数
            if (lastRecord.getDuanshaErr() == 1 && huafangZhengjing.getDuanshaErr() == 0) {
                huafangZhengjing.setStopNum(DataCollectingApplication.stopNum.get(ip) + 1);
                DataCollectingApplication.stopNum.put(ip, DataCollectingApplication.stopNum.get(ip) + 1);

            }
        }
        HuafangZhengjingHis huafangZhengjingHis = (HuafangZhengjingHis) Data.bind(new HuafangZhengjingHis(), map);
        if (now < last) {
            hisService.save(DataCollectingApplication.huafangZhengjingHis.get(ip));
            DataCollectingApplication.huafangZhengjingHis.put(ip, huafangZhengjingHis);
        } else{
            DataCollectingApplication.huafangZhengjingHis.put(ip, huafangZhengjingHis);
        }

        return huafangZhengjing;
    }

    @Override
    public IService getService() {
        return service;
    }

    private boolean hasInt(Integer i, Integer[] ints) {
        return Arrays.asList(ints).contains(i);
    }

}
